bits 16
org 0x7c00

EnableA20:
xor cx,cx
mov cx,5
pusha
cli

A20start:
waitForWrite1:
xor ax,ax
in al,0x64
bt ax,1
jc waitForWrite1

xor ax,ax
mov al,0xD0
out 0x64,al

waitForData1:
xor ax,ax
in al,0x64
bt ax,0
jnc waitForData1

xor ax,ax
in al,0x60
push ax

waitForWrite2:
xor ax,ax
in al,0x64
bt ax,1
jc waitForWrite2

xor ax,ax
mov al,0xD1
out 0x64,al

waitForWrite3:
xor ax,ax
in al,0x64
bt ax, 1
jc waitForWrite3

pop ax
or al,00000010b
out 0x60,al

waitForWrite4:
xor ax,ax
in al,0x64
bt ax,1
jc waitForWrite4

xor ax,ax
mov al,0xD0
out 0x64,al

waitForData3:
xor ax,ax
in al,0x64
bt ax,0
jnc waitForData3

xor ax,ax
in al, 0x60
bt ax,1
jc succes

loop A20start
jmp error

succes
popa
sti
mov eax,0
ret

error:
popa
sti
mov eax,-1
ret

times 510-($-$$) db  0
dw 0xAA55

